package com.heying.springcore;

import com.heying.springcore.javaconfig.Car;
import com.heying.springcore.javaconfig.Driver;
import com.heying.springcore.javaconfig.Student;
import com.heying.springcore.javaconfig.Tank;
import com.heying.springcore.xml.Vehicle;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * 不再是一个普通的类
 * 是用来配置文件的类
 * 相当于applicationContext.xml
 */
@Configuration
@ComponentScan
@ImportResource("classpath:applicationContext.xml")
@PropertySource("classpath:spring.properties")
@EnableAspectJAutoProxy
public class SpringConfig {

    @Bean//默认的名字就是方法名
    public Driver driver(Vehicle car){   //注入的默认的参数名就是bean
        return new Driver("ttt", 33, car);
    }

    @Bean
    public Car car(){
        return new Car("BTR", "黑色", 33333);
    }

    @Bean
    public Tank tank(){
        return new Tank("中式", "蓝色", 99999);
    }

    @Bean
    public Student borrower(Student lender){
        Student borrower = new Student("张三", 100);
        borrower.setStu(lender);
        return borrower;
    }

    @Bean
    public Student lender(){
        Student lender = new Student("李四", 800);
        return lender;

    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer
    placeholderConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds.setUrl("jdbc:mysql://localhost:3306/test02?userSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8");
        ds.setUsername("root");
        ds.setPassword("");
        return ds;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}


